﻿using Newtonsoft.Json;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Model.PhysicalExamination;
using PhysicalExamination5.Model.SystemSetting;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PhysicalExamination5.Service.DataMaintenance
{
    public class SZuHeXiangMuWeiHu : IZuHeXiangMuWeiHu
    {
        private readonly ISqlSugarClient db;
        public SZuHeXiangMuWeiHu(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 组合项目维护申请单添加
        /// </summary>
        /// <param name="ShenQingDanAddClass"></param>
        /// <returns></returns>
        public string PostShenQingDanAdd(ShenQingDanAddClass ShenQingDanAddClass, string strYuanQuQianZhui)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false; int iShunXu = 0; string strSql = ""; string strShenQingDanID = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{ShenQingDanAddClass}】");

                #region 数据校验

                if (string.IsNullOrEmpty(ShenQingDanAddClass.KeShiID))
                {
                    nret = 1;
                    strFanHuiZhi = "科室ID为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ShenQingDanAddClass.ShenQingDanMingCheng))
                {
                    nret = 1;
                    strFanHuiZhi = "申请单名称为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ShenQingDanAddClass.SuoShuYuanQu))
                {
                    nret = 1;
                    strFanHuiZhi = "所属院区为空，请检查！";
                    goto PROC_EXIT;
                }

                //小结文字正常描述如果为空，默认为未见明显异常

                if (string.IsNullOrEmpty(ShenQingDanAddClass.ZhengChangMiaoShu))
                {
                    ShenQingDanAddClass.ZhengChangMiaoShu = "未见明显异常";
                }
                //打印次数如果为0，默认为1
                if (ShenQingDanAddClass.DaYinCiShu == 0)
                {
                    ShenQingDanAddClass.DaYinCiShu = 1;
                }

                #endregion

                #region 判断是否有相同名称的申请单

                List<jcsj_ShenQingDan> ListShenQingDan = new SqlSugarRepository<jcsj_ShenQingDan>(db).getByWhere(a => a.ShenQingDanMingCheng == ShenQingDanAddClass.ShenQingDanMingCheng);

                if (ListShenQingDan.Count != 0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前申请单【{ShenQingDanAddClass.ShenQingDanMingCheng}】已存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 获得顺序

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    List<jcsj_ShenQingDan> ListXinXi = new SqlSugarRepository<jcsj_ShenQingDan>(db).getAll();

                    if (ListXinXi.Count != 0)
                    {
                        iShunXu = ListXinXi.Max(t => t.ShunXu) + 1;
                    }
                    else
                    {
                        iShunXu = 1;
                    }

                    #endregion

                    #region 执行插入

                    jcsj_ShenQingDan ShenQingDan = new jcsj_ShenQingDan();

                    ShenQingDan.KeShiID = ShenQingDanAddClass.KeShiID.Trim();
                    ShenQingDan.SuoShuYuanQu = ShenQingDanAddClass.SuoShuYuanQu.Trim();
                    ShenQingDan.ShenQingDanMingCheng = ShenQingDanAddClass.ShenQingDanMingCheng.Trim();
                    ShenQingDan.JianYanXingZhi = ShenQingDanAddClass.JianYanXingZhi.Trim();
                    ShenQingDan.ZhiXingZu = ShenQingDanAddClass.ZhiXingZu.Trim();
                    ShenQingDan.ShiGuanLeiXing = ShenQingDanAddClass.ShiGuanLeiXing.Trim();
                    ShenQingDan.DaYinCiShu = ShenQingDanAddClass.DaYinCiShu;
                    ShenQingDan.ShunXu = iShunXu;
                    ShenQingDan.ZhiXingKeShiMingCheng = ShenQingDanAddClass.ZhiXingKeShiMingCheng.Trim();
                    ShenQingDan.SheBeiBiaoShi = ShenQingDanAddClass.SheBeiBiaoShi.Trim();
                    ShenQingDan.JianChaBuWei = ShenQingDanAddClass.JianChaBuWei.Trim();
                    ShenQingDan.JianChaLeiBie = ShenQingDanAddClass.JianChaLeiBie.Trim();
                    ShenQingDan.BeiZhu1 = ShenQingDanAddClass.BeiZhu1.Trim();
                    ShenQingDan.BeiZhu2 = ShenQingDanAddClass.BeiZhu2.Trim();
                    ShenQingDan.BeiZhu3 = ShenQingDanAddClass.BeiZhu3.Trim();

                    //获取到院区前缀并且加上返回来的tid
                    strShenQingDanID = strYuanQuQianZhui + new SqlSugarRepository<jcsj_ShenQingDan>(db).AddReturnIdentity(ShenQingDan);

                    #region 执行修改语句将申请单ID修改进去

                    strSql = $"update jcsj_ShenQingDan set ShenQingDanID='{strShenQingDanID}' where ShenQingDanMingCheng='{ShenQingDanAddClass.ShenQingDanMingCheng}'";

                    BolShiFouChengGong = new SqlSugarRepository<jcsj_KeShi>(db).ExecuteCommandSql(strSql);

                    if (BolShiFouChengGong == false)
                    {
                        nret = 1;
                        strFanHuiZhi = "修改申请单ID失败，请检查！";
                        return;
                    }

                    #endregion

                    #endregion

                    #region 处理jcsj_ShenQingDanFenYuanQu

                    string[] ArrYuanQu = ShenQingDanAddClass.SuoShuYuanQu.Split(';');

                    foreach (string strYuanQuMingCheng in ArrYuanQu)
                    {
                        if (string.IsNullOrEmpty(strYuanQuMingCheng) == false)
                        {
                            #region 执行删除原有数据

                            //先删除后插入
                            BolShiFouChengGong = new SqlSugarRepository<jcsj_ShenQingDanFenYuanQu>(db).DeleteWhere(A => A.SuoShuYuanQu == strYuanQuMingCheng && A.ShenQingDanID == strShenQingDanID);

                            if (BolShiFouChengGong == false)
                            {
                                nret = 1;
                                strFanHuiZhi = "删除申请单分院区失败，请检查！";
                                return;
                            }

                            #endregion

                            #region 执行增加jcsj_ShenQingDanFenYuanQu

                            jcsj_ShenQingDanFenYuanQu ShenQingDanFenYuanQu = new jcsj_ShenQingDanFenYuanQu();

                            ShenQingDanFenYuanQu.ShenQingDanID = strShenQingDanID;
                            ShenQingDanFenYuanQu.SuoShuYuanQu = strYuanQuMingCheng;
                            ShenQingDanFenYuanQu.TiShiXinXi = ShenQingDanAddClass.ZhiYinDanTiShiXinXi;
                            ShenQingDanFenYuanQu.ZhengChangMiaoShu = ShenQingDanAddClass.ZhengChangMiaoShu;
                            ShenQingDanFenYuanQu.MoRenJianChaZhe = ShenQingDanAddClass.MoRenJianChaZhe;
                            ShenQingDanFenYuanQu.MoRenShenHeZhe = ShenQingDanAddClass.MoRenShenHeZhe;

                            BolShiFouChengGong = new SqlSugarRepository<jcsj_ShenQingDanFenYuanQu>(db).Add(ShenQingDanFenYuanQu);
                            if (BolShiFouChengGong == false)
                            {
                                nret = 1;
                                strFanHuiZhi = "添加申请单分院区出现错误，请检查！";
                                return;
                            }

                            #endregion
                        }
                    }
                },
             (ex) =>
             {
                 nret = 1000;
                 strFanHuiZhi = ex.Message;
                 Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
             });
            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 组合项目维护申请修改
        /// </summary>
        /// <param name="ShenQingDanUpdateClass"></param>
        /// <returns></returns>
        public string PutShenSingDanUpdate(ShenQingDanUpdateClass ShenQingDanUpdateClass)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false; string strSql = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{ShenQingDanUpdateClass}】");

                #region 数据校验

                if (string.IsNullOrEmpty(ShenQingDanUpdateClass.ShenQingDanMingCheng))
                {
                    nret = 1;
                    strFanHuiZhi = "申请单名称为空，请检查！";
                    goto PROC_EXIT;
                }

                if (string.IsNullOrEmpty(ShenQingDanUpdateClass.ShenQingDanID))
                {
                    nret = 1;
                    strFanHuiZhi = "申请单ID为空，请检查！";
                    goto PROC_EXIT;
                }

                if (string.IsNullOrEmpty(ShenQingDanUpdateClass.SuoShuYuanQu))
                {
                    nret = 1;
                    strFanHuiZhi = "所属院区为空，请检查！";
                    goto PROC_EXIT;
                }

                //小结文字正常描述如果为空，默认为未见明显异常

                if (string.IsNullOrEmpty(ShenQingDanUpdateClass.ZhengChangMiaoShu))
                {
                    ShenQingDanUpdateClass.ZhengChangMiaoShu = "未见明显异常";
                }
                //打印次数如果为0，默认为1
                if (ShenQingDanUpdateClass.DaYinCiShu == 0)
                {
                    ShenQingDanUpdateClass.DaYinCiShu = 1;
                }

                #endregion

                #region 判断是否有相同名称的申请单

                List<jcsj_ShenQingDan> ListShenQingDan = new SqlSugarRepository<jcsj_ShenQingDan>(db).getByWhere(a => a.ShenQingDanMingCheng == ShenQingDanUpdateClass.ShenQingDanMingCheng && a.ShenQingDanID != ShenQingDanUpdateClass.ShenQingDanID);

                if (ListShenQingDan.Count != 0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前申请单【{ShenQingDanUpdateClass.ShenQingDanMingCheng}】已存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行修改

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {

                    strSql = $@"update jcsj_ShenQingDan set SuoShuYuanQu='{ShenQingDanUpdateClass.SuoShuYuanQu}',
ShenQingDanMingCheng='{ShenQingDanUpdateClass.ShenQingDanMingCheng}',JianYanXingZhi='{ShenQingDanUpdateClass.JianYanXingZhi}',
ZhiXingZu='{ShenQingDanUpdateClass.ZhiXingZu}',ShiGuanLeiXing='{ShenQingDanUpdateClass.ShiGuanLeiXing}',DaYinCiShu='{ShenQingDanUpdateClass.DaYinCiShu}',
ZhiXingKeShiMingCheng='{ShenQingDanUpdateClass.ZhiXingKeShiMingCheng}',SheBeiBiaoShi='{ShenQingDanUpdateClass.SheBeiBiaoShi}',
JianChaBuWei='{ShenQingDanUpdateClass.JianChaBuWei}',JianChaLeiBie='{ShenQingDanUpdateClass.JianChaLeiBie}',
BeiZhu1='{ShenQingDanUpdateClass.BeiZhu1}',BeiZhu2='{ShenQingDanUpdateClass.BeiZhu2}',BeiZhu3='{ShenQingDanUpdateClass.BeiZhu3}'
where ShenQingDanID='{ShenQingDanUpdateClass.ShenQingDanID}'";

                    BolShiFouChengGong = new SqlSugarRepository<jcsj_ShenQingDan>(db).ExecuteCommandSql(strSql);

                    if (BolShiFouChengGong == false)
                    {
                        nret = 1;
                        strFanHuiZhi = "修改申请单失败，请检查";
                        return;
                    }

                    #endregion

                    #region 处理jcsj_ShenQingDanFenYuanQu

                    string[] ArrYuanQu = ShenQingDanUpdateClass.SuoShuYuanQu.Split(';');

                    foreach (string strYuanQuMingCheng in ArrYuanQu)
                    {
                        if (string.IsNullOrEmpty(strYuanQuMingCheng) == false)
                        {
                            #region 执行删除原有数据

                            //先删除后插入
                            BolShiFouChengGong = new SqlSugarRepository<jcsj_ShenQingDanFenYuanQu>(db).DeleteWhere(A => A.SuoShuYuanQu == strYuanQuMingCheng && A.ShenQingDanID == ShenQingDanUpdateClass.ShenQingDanID);

                            if (BolShiFouChengGong == false)
                            {
                                nret = 1;
                                strFanHuiZhi = "删除申请单分院区失败，请检查！";
                                return;
                            }

                            #endregion

                            #region 执行增加jcsj_ShenQingDanFenYuanQu

                            jcsj_ShenQingDanFenYuanQu ShenQingDanFenYuanQu = new jcsj_ShenQingDanFenYuanQu();

                            ShenQingDanFenYuanQu.ShenQingDanID = ShenQingDanUpdateClass.ShenQingDanID;
                            ShenQingDanFenYuanQu.SuoShuYuanQu = strYuanQuMingCheng;
                            ShenQingDanFenYuanQu.TiShiXinXi = ShenQingDanUpdateClass.ZhiYinDanTiShiXinXi;
                            ShenQingDanFenYuanQu.ZhengChangMiaoShu = ShenQingDanUpdateClass.ZhengChangMiaoShu;
                            ShenQingDanFenYuanQu.MoRenJianChaZhe = ShenQingDanUpdateClass.MoRenJianChaZhe;
                            ShenQingDanFenYuanQu.MoRenShenHeZhe = ShenQingDanUpdateClass.MoRenShenHeZhe;

                            BolShiFouChengGong = new SqlSugarRepository<jcsj_ShenQingDanFenYuanQu>(db).Add(ShenQingDanFenYuanQu);
                            if (BolShiFouChengGong == false)
                            {
                                nret = 1;
                                strFanHuiZhi = "添加申请单分院区出现错误，请检查！";
                                return;
                            }

                            #endregion
                        }
                    }
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });
            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 组合项目维护申请删除
        /// </summary>
        /// <param name="strShenQingDanID"></param>
        /// <returns></returns>
        public string DeleteShenQingDanDel(string strShenQingDanID)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false;
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{strShenQingDanID}】");

                #region 数据校验

                if (string.IsNullOrEmpty(strShenQingDanID))
                {
                    nret = 1;
                    strFanHuiZhi = "申请单ID为空，请检查！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 若科室下存在申请单则不允许删除

                List<jcsj_ZuHeXiangMu> ZuHeXiangMu = new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).getByWhere(a => a.ShenQingDanID == strShenQingDanID);

                if (ZuHeXiangMu.Count != 0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前申请单已有组合项目已存在，不允许删除！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行删除

                //删除jcsj_ShenQingDan
                BolShiFouChengGong = new SqlSugarRepository<jcsj_ShenQingDan>(db).DeleteWhere(A => A.ShenQingDanID == strShenQingDanID);
                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "申请单删除失败，请检查!";
                    goto PROC_EXIT;
                }
                //删除jcsj_ShenQingDanFenYuanQu
                BolShiFouChengGong = new SqlSugarRepository<jcsj_ShenQingDanFenYuanQu>(db).DeleteWhere(A => A.ShenQingDanID == strShenQingDanID);
                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "申请单分院区删除失败，请检查!";
                    goto PROC_EXIT;
                }

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 根据科室ID、所属院区查询申请单
        /// </summary>
        /// <param name="strKeShiID"></param>
        /// <param name="strSuoShuYuanQu"></param>
        /// <returns></returns>
        public string GetShenQingDanSel(string strKeShiID, string strSuoShuYuanQu)
        {
            int nret = 0; string strFanHuiZhi = ""; string strSql = "";
            List<object> ListShenQingDan = new List<object>();
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{strKeShiID},{strSuoShuYuanQu}】");

                #region 校验数据

                if (string.IsNullOrEmpty(strKeShiID))
                {
                    nret = 1;
                    strFanHuiZhi = "科室ID为空，请检查！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行查询

                strSql = $@"
select A.*,
b.TiShiXinXi,
b.ZhengChangMiaoShu,
b.MoRenJianChaZhe,
b.MoRenShenHeZhe 
from jcsj_ShenQingDan a, jcsj_ShenQingDanFenYuanQu b 
where a.ShenQingDanID=b.ShenQingDanID 
and a.KeShiID='{strKeShiID}'
and b.SuoShuYuanQu='{strSuoShuYuanQu}'
and a.SuoShuYuanQu like'%{strSuoShuYuanQu}%'
order by a.ShunXu
";
                ListShenQingDan = new SqlSugarRepository<object>(db).getListBySql(strSql);

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ListShenQingDan

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 组合项目维护添加
        /// </summary>
        /// <param name="ZuHeXiangMuAddClass"></param>
        /// <returns></returns>
        public string PostZuHeXiangMuAdd(ZuHeXiangMuAddClass ZuHeXiangMuAddClass, string YuanQuQianZhui)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false; int iShunXu = 0; string strSql = ""; string strZuHeXiangMuID = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{ZuHeXiangMuAddClass}】");

                #region 数据校验

                if (string.IsNullOrEmpty(ZuHeXiangMuAddClass.KeShiID))
                {
                    nret = 1;
                    strFanHuiZhi = "科室ID为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuAddClass.ShenQingDanID))
                {
                    nret = 1;
                    strFanHuiZhi = "申请单ID为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuAddClass.SuoShuYuanQu))
                {
                    nret = 1;
                    strFanHuiZhi = "所属院区为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuAddClass.ShiFouWeiXinZhanShi))
                {
                    nret = 1;
                    strFanHuiZhi = "是否微信展示为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuAddClass.ShiFouZongjianZhanShi))
                {
                    nret = 1;
                    strFanHuiZhi = "是否总检展示为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuAddClass.ShiFouQiYong))
                {
                    nret = 1;
                    strFanHuiZhi = "是否启用为空，请检查！";
                    goto PROC_EXIT;
                }

                if (string.IsNullOrEmpty(ZuHeXiangMuAddClass.BaoGaoSuoShuFenLei))
                {
                    ZuHeXiangMuAddClass.BaoGaoSuoShuFenLei = "公用";
                }

                if (string.IsNullOrEmpty(ZuHeXiangMuAddClass.ShiYongXingBie))
                {
                    ZuHeXiangMuAddClass.ShiYongXingBie = "不限";
                }

                #endregion

                #region 判断是否存在相同名称的组合项目

                List<jcsj_ZuHeXiangMu> ListZuHeXiangMu = new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).getByWhere(a => a.ZuHeXiangMuMingCheng == ZuHeXiangMuAddClass.ZuHeXiangMuMingCheng);

                if (ListZuHeXiangMu.Count != 0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前组合项目【{ZuHeXiangMuAddClass.ZuHeXiangMuMingCheng}】已存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 获得顺序

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    List<jcsj_ZuHeXiangMu> ListXinXi = new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).getByWhere(t => t.ShenQingDanID == ZuHeXiangMuAddClass.ShenQingDanID);

                    if (ListXinXi.Count != 0)
                    {
                        iShunXu = ListXinXi.Max(t => t.ShunXu) + 1;
                    }
                    else
                    {
                        iShunXu = 1;
                    }

                    #endregion

                    #region 执行插入

                    jcsj_ZuHeXiangMu ZuHeXiangMu = new jcsj_ZuHeXiangMu();
                    ZuHeXiangMu.KeShiID = ZuHeXiangMuAddClass.KeShiID.Trim();
                    ZuHeXiangMu.ShenQingDanID = ZuHeXiangMuAddClass.ShenQingDanID.Trim();
                    ZuHeXiangMu.SuoShuYuanQu = ZuHeXiangMuAddClass.SuoShuYuanQu.Trim();
                    ZuHeXiangMu.ZuHeXiangMuMingCheng = ZuHeXiangMuAddClass.ZuHeXiangMuMingCheng.Trim();
                    ZuHeXiangMu.ZhengChangMiaoShu = ZuHeXiangMuAddClass.ZhengChangMiaoShu.Trim();
                    ZuHeXiangMu.FeiYongLeiBie = ZuHeXiangMuAddClass.FeiYongLeiBie.Trim();
                    ZuHeXiangMu.GuoBiaoDaiMa = ZuHeXiangMuAddClass.GuoBiaoDaiMa.Trim();
                    ZuHeXiangMu.ShangJiGuoBiaoDaiMa = ZuHeXiangMuAddClass.ShangJiGuoBiaoDaiMa.Trim();
                    ZuHeXiangMu.ShangJiGuoBiaoMingCheng = ZuHeXiangMuAddClass.ShangJiGuoBiaoMingCheng.Trim();
                    ZuHeXiangMu.BaoGaoSuoShuFenLei = ZuHeXiangMuAddClass.BaoGaoSuoShuFenLei.Trim();
                    ZuHeXiangMu.PYM = ZuHeXiangMuAddClass.PYM.Trim();
                    ZuHeXiangMu.HISKeShiDaiMa = ZuHeXiangMuAddClass.HISKeShiDaiMa.Trim();
                    ZuHeXiangMu.HISKeShiMingCheng = ZuHeXiangMuAddClass.HISKeShiMingCheng.Trim();
                    ZuHeXiangMu.YiXueJianMa = ZuHeXiangMuAddClass.YiXueJianMa.Trim();
                    ZuHeXiangMu.YiXueMingCheng = ZuHeXiangMuAddClass.YiXueMingCheng.Trim();
                    ZuHeXiangMu.ShiYongXingBie = ZuHeXiangMuAddClass.ShiYongXingBie.Trim();
                    ZuHeXiangMu.BeiZhu1 = ZuHeXiangMuAddClass.BeiZhu1.Trim();
                    ZuHeXiangMu.BeiZhu2 = ZuHeXiangMuAddClass.BeiZhu2.Trim();
                    ZuHeXiangMu.BeiZhu3 = ZuHeXiangMuAddClass.BeiZhu3.Trim();
                    ZuHeXiangMu.ShiFouWeiXinZhanShi = ZuHeXiangMuAddClass.ShiFouWeiXinZhanShi.Trim();
                    ZuHeXiangMu.ShiFouZongjianZhanShi = ZuHeXiangMuAddClass.ShiFouZongjianZhanShi.Trim();
                    ZuHeXiangMu.ShiFouQiYong = ZuHeXiangMuAddClass.ShiFouQiYong.Trim();
                    ZuHeXiangMu.ShiFouZaiFenJianXianShiMingXi = ZuHeXiangMuAddClass.ShiFouZaiFenJianXianShiMingXi.Trim();
                    ZuHeXiangMu.JianChaZhiBiaoYiYi = ZuHeXiangMuAddClass.JianChaZhiBiaoYiYi.Trim();
                    ZuHeXiangMu.ShunXu = iShunXu;
                    ZuHeXiangMu.WaiJieBianMa = ZuHeXiangMuAddClass.WaiJieBianMa.Trim();

                    //获取到院区前缀并且加上返回来的tid
                    strZuHeXiangMuID = YuanQuQianZhui + new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).AddReturnIdentity(ZuHeXiangMu);

                    #region 执行修改语句将组合项目ID修改进去

                    strSql = $"update jcsj_ZuHeXiangMu set ZuHeXiangMuID='{strZuHeXiangMuID}' where ZuHeXiangMuMingCheng='{ZuHeXiangMuAddClass.ZuHeXiangMuMingCheng}'";

                    BolShiFouChengGong = new SqlSugarRepository<jcsj_KeShi>(db).ExecuteCommandSql(strSql);

                    if (BolShiFouChengGong == false)
                    {
                        nret = 1;
                        strFanHuiZhi = "修改组合ID失败，请检查！";
                        return;
                    }

                    #endregion

                    #endregion

                    #region 处理jcsj_ZuHeXiangMuFenYuanQu

                    string[] ArrYuanQu = ZuHeXiangMuAddClass.SuoShuYuanQu.Split(';');

                    foreach (string strYuanQuMingCheng in ArrYuanQu)
                    {
                        if (string.IsNullOrEmpty(strYuanQuMingCheng) == false)
                        {
                            #region 执行删除原有数据

                            //先删除后插入
                            BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMuFenYuanQu>(db).DeleteWhere(A => A.SuoShuYuanQu == strYuanQuMingCheng && A.ZuHeXiangMuID == strZuHeXiangMuID);

                            if (BolShiFouChengGong == false)
                            {
                                nret = 1;
                                strFanHuiZhi = "删除组合项目分院区失败，请检查！";
                                return;
                            }

                            #endregion

                            #region 执行增加jcsj_ZuHeXiangMuFenYuanQu

                            jcsj_ZuHeXiangMuFenYuanQu ZuHeXiangMuFenYuanQu = new jcsj_ZuHeXiangMuFenYuanQu();

                            ZuHeXiangMuFenYuanQu.ZuHeXiangMuID = strZuHeXiangMuID;
                            ZuHeXiangMuFenYuanQu.SuoShuYuanQu = strYuanQuMingCheng;
                            ZuHeXiangMuFenYuanQu.DanJia = ZuHeXiangMuAddClass.DanJian;
                            ZuHeXiangMuFenYuanQu.YouHuiJia = ZuHeXiangMuAddClass.YouHuiJian;
                            ZuHeXiangMuFenYuanQu.ChengBenjia = ZuHeXiangMuAddClass.ChengBenJian;
                            ZuHeXiangMuFenYuanQu.KeShiFenChengJia = ZuHeXiangMuAddClass.KeShiFenChengJia;
                            ZuHeXiangMuFenYuanQu.TiShiXinXi = ZuHeXiangMuAddClass.TiShiXinXi;
                            ZuHeXiangMuFenYuanQu.MoRenShenHeZhe = ZuHeXiangMuAddClass.MoRenShenHeZhe;
                            ZuHeXiangMuFenYuanQu.MoRenJianChaZhe = ZuHeXiangMuAddClass.MoRenJianChaZhe;
                            ZuHeXiangMuFenYuanQu.ShiFouDaoRuWaiJie = ZuHeXiangMuAddClass.ShiFouDaoRuWaiJie;

                            BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMuFenYuanQu>(db).Add(ZuHeXiangMuFenYuanQu);
                            if (BolShiFouChengGong == false)
                            {
                                nret = 1;
                                strFanHuiZhi = "添加申请单分院区出现错误，请检查！";
                                return;
                            }

                            #endregion
                        }
                    }
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 组合项目维护修改
        /// </summary>
        /// <param name="ZuHeXiangMuUpdateClass"></param>
        /// <returns></returns>
        public string PutZuHeXiangMuUpdate(ZuHeXiangMuUpdateClass ZuHeXiangMuUpdateClass)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false; string strSql = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{ZuHeXiangMuUpdateClass}】");

                #region 校验数据

                if (string.IsNullOrEmpty(ZuHeXiangMuUpdateClass.KeShiID))
                {
                    nret = 1;
                    strFanHuiZhi = "科室ID为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuUpdateClass.ShenQingDanID))
                {
                    nret = 1;
                    strFanHuiZhi = "申请单ID为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuUpdateClass.SuoShuYuanQu))
                {
                    nret = 1;
                    strFanHuiZhi = "所属院区为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuUpdateClass.ShiFouWeiXinZhanShi))
                {
                    nret = 1;
                    strFanHuiZhi = "是否微信展示为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuUpdateClass.ShiFouZongjianZhanShi))
                {
                    nret = 1;
                    strFanHuiZhi = "是否总检展示为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(ZuHeXiangMuUpdateClass.ShiFouQiYong))
                {
                    nret = 1;
                    strFanHuiZhi = "是否启用为空，请检查！";
                    goto PROC_EXIT;
                }


                if (string.IsNullOrEmpty(ZuHeXiangMuUpdateClass.BaoGaoSuoShuFenLei))
                {
                    ZuHeXiangMuUpdateClass.BaoGaoSuoShuFenLei = "公用";
                }

                if (string.IsNullOrEmpty(ZuHeXiangMuUpdateClass.ShiYongXingBie))
                {
                    ZuHeXiangMuUpdateClass.ShiYongXingBie = "不限";
                }

                #endregion

                #region 判断是否有相同名称的申请单

                List<jcsj_ZuHeXiangMu> ListZuHeXiangMu = new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).getByWhere(a => a.ZuHeXiangMuMingCheng == ZuHeXiangMuUpdateClass.ZuHeXiangMuMingCheng && a.ZuHeXiangMuID != ZuHeXiangMuUpdateClass.ZuHeXiangMuID);

                if (ListZuHeXiangMu.Count != 0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前组合项目【{ZuHeXiangMuUpdateClass.ZuHeXiangMuID}】已存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行修改

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    strSql = $@"update jcsj_ZuHeXiangMu set ZuHeXiangMuMingCheng='{ZuHeXiangMuUpdateClass.ZuHeXiangMuMingCheng}',ShiYongXingBie='{ZuHeXiangMuUpdateClass.ShiYongXingBie}', 
SuoShuYuanQu='{ZuHeXiangMuUpdateClass.SuoShuYuanQu}',ZhengChangMiaoShu='{ZuHeXiangMuUpdateClass.ZhengChangMiaoShu}',FeiYongLeiBie='{ZuHeXiangMuUpdateClass.FeiYongLeiBie}',
GuoBiaoDaiMa='{ZuHeXiangMuUpdateClass.GuoBiaoDaiMa}',ShangJiGuoBiaoDaiMa='{ZuHeXiangMuUpdateClass.ShangJiGuoBiaoDaiMa}',ShangJiGuoBiaoMingCheng='{ZuHeXiangMuUpdateClass.ShangJiGuoBiaoMingCheng}',
BaoGaoSuoShuFenLei='{ZuHeXiangMuUpdateClass.BaoGaoSuoShuFenLei}',PYM='{ZuHeXiangMuUpdateClass.PYM}',WaiJieBianMa='{ZuHeXiangMuUpdateClass.WaiJieBianMa}',
HISKeShiDaiMa='{ZuHeXiangMuUpdateClass.HISKeShiDaiMa}',HISKeShiMingCheng='{ZuHeXiangMuUpdateClass.HISKeShiMingCheng}',
YiXueMingCheng='{ZuHeXiangMuUpdateClass.YiXueMingCheng}',YiXueJianMa='{ZuHeXiangMuUpdateClass.YiXueJianMa}',
BeiZhu1='{ZuHeXiangMuUpdateClass.BeiZhu1}',BeiZhu2='{ZuHeXiangMuUpdateClass.BeiZhu2}',BeiZhu3='{ZuHeXiangMuUpdateClass.BeiZhu3}',
ShiFouWeiXinZhanShi='{ZuHeXiangMuUpdateClass.ShiFouQiYong}',ShiFouZongjianZhanShi='{ZuHeXiangMuUpdateClass.ShiFouZongjianZhanShi}',
ShiFouQiYong='{ZuHeXiangMuUpdateClass.ShiFouQiYong}',ShiFouZaiFenJianXianShiMingXi='{ZuHeXiangMuUpdateClass.ShiFouZaiFenJianXianShiMingXi}',
JianChaZhiBiaoYiYi='{ZuHeXiangMuUpdateClass.JianChaZhiBiaoYiYi}'
where ZuHeXiangMuID='{ZuHeXiangMuUpdateClass.ZuHeXiangMuID}'";

                    BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).ExecuteCommandSql(strSql);

                    if (BolShiFouChengGong == false)
                    {
                        nret = 1;
                        strFanHuiZhi = "修改组合项目失败，请检查";
                        return;
                    }

                    #endregion

                    #region 处理jcsj_ZuHeXiangMuFenYuanQu

                    string[] ArrYuanQu = ZuHeXiangMuUpdateClass.SuoShuYuanQu.Split(';');

                    foreach (string strYuanQuMingCheng in ArrYuanQu)
                    {
                        if (string.IsNullOrEmpty(strYuanQuMingCheng) == false)
                        {
                            #region 执行删除原有数据

                            //先删除后插入
                            BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMuFenYuanQu>(db).DeleteWhere(A => A.SuoShuYuanQu == strYuanQuMingCheng && A.ZuHeXiangMuID == ZuHeXiangMuUpdateClass.ZuHeXiangMuID);

                            if (BolShiFouChengGong == false)
                            {
                                nret = 1;
                                strFanHuiZhi = "删除组合项目分院区失败，请检查！";
                                return;
                            }

                            #endregion

                            #region 执行增加jcsj_ZuHeXiangMuFenYuanQu

                            jcsj_ZuHeXiangMuFenYuanQu ZuHeXiangMuFenYuanQu = new jcsj_ZuHeXiangMuFenYuanQu();

                            ZuHeXiangMuFenYuanQu.ZuHeXiangMuID = ZuHeXiangMuUpdateClass.ZuHeXiangMuID;
                            ZuHeXiangMuFenYuanQu.SuoShuYuanQu = strYuanQuMingCheng;
                            ZuHeXiangMuFenYuanQu.DanJia = ZuHeXiangMuUpdateClass.DanJian;
                            ZuHeXiangMuFenYuanQu.YouHuiJia = ZuHeXiangMuUpdateClass.YouHuiJian;
                            ZuHeXiangMuFenYuanQu.ChengBenjia = ZuHeXiangMuUpdateClass.ChengBenJian;
                            ZuHeXiangMuFenYuanQu.KeShiFenChengJia = ZuHeXiangMuUpdateClass.KeShiFenChengJia;
                            ZuHeXiangMuFenYuanQu.TiShiXinXi = ZuHeXiangMuUpdateClass.TiShiXinXi;
                            ZuHeXiangMuFenYuanQu.MoRenShenHeZhe = ZuHeXiangMuUpdateClass.MoRenShenHeZhe;
                            ZuHeXiangMuFenYuanQu.MoRenJianChaZhe = ZuHeXiangMuUpdateClass.MoRenJianChaZhe;
                            ZuHeXiangMuFenYuanQu.ShiFouDaoRuWaiJie = ZuHeXiangMuUpdateClass.ShiFouDaoRuWaiJie;

                            BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMuFenYuanQu>(db).Add(ZuHeXiangMuFenYuanQu);
                            if (BolShiFouChengGong == false)
                            {
                                nret = 1;
                                strFanHuiZhi = "修改申请单分院区出现错误，请检查！";
                                return;
                            }

                            #endregion
                        }
                    }
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });
            #endregion



            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 组合项目维护删除
        /// </summary>
        /// <param name="strZuHeXiangMuID"></param>
        /// <returns></returns>
        public string DeleteZuHeXiangMuDel(string strZuHeXiangMuID)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false;
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{strZuHeXiangMuID}】");

                #region 数据校验

                if (string.IsNullOrEmpty(strZuHeXiangMuID))
                {
                    nret = 1;
                    strFanHuiZhi = "组合项目ID为空，请检查！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 若组合已经使用不可删除

                //判断组合项目是否已使用

                List<ry_ShenQingDanMingXi> ShenQingDanMingXi = new SqlSugarRepository<ry_ShenQingDanMingXi>(db).getByWhere(a => a.ZuHeXiangMuID == strZuHeXiangMuID);

                if (ShenQingDanMingXi.Count != 0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前组合项目已使用，不允许删除！";
                    goto PROC_EXIT;
                }

                //判断危害因素是否使用组合项目

                List<jcsj_WeiHaiYinSuZuHeXiangMu> WeiHaiYinSuZuHe = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).getByWhere(a => a.ZuHeXiangMuID == strZuHeXiangMuID);

                if (WeiHaiYinSuZuHe.Count != 0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前组合项目已使用，不允许删除！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行删除

                //删除jcsj_ZuHeXiangMu
                BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).DeleteWhere(A => A.ZuHeXiangMuID == strZuHeXiangMuID);
                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "组合项目删除失败，请检查!";
                    goto PROC_EXIT;
                }

                //删除jcsj_MingXiXiangMu

                BolShiFouChengGong = new SqlSugarRepository<jcsj_MingXiXiangMu>(db).DeleteWhere(A => A.ZuHeXiangMuID == strZuHeXiangMuID);
                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "明细项目删除失败，请检查!";
                    goto PROC_EXIT;
                }

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 根据申请单ID、所属报告分类查询组合项目
        /// </summary>
        /// <param name="strShenQingDanID"></param>
        /// <param name="strSuoShuBaoGaoFenLei"></param>
        /// <param name="YuanQu"></param>
        /// <param name="intPage"></param>
        /// <param name="intPageSize"></param>
        /// <returns></returns>
        public string GetZuHeXiangMuSel(string strShenQingDanID, string strSuoShuBaoGaoFenLei, string YuanQu)
        {
            int nret = 0; string strFanHuiZhi = ""; List<object> ListZuHeXiangMu = new List<object>();
            string strSql = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{strShenQingDanID},{strSuoShuBaoGaoFenLei},{YuanQu}】");

                #region 校验数据

                if (string.IsNullOrEmpty(strShenQingDanID))
                {
                    nret = 1;
                    strFanHuiZhi = "申请单ID为空，请检查！";
                    goto PROC_EXIT;
                }

                if (string.IsNullOrEmpty(strSuoShuBaoGaoFenLei))
                {
                    nret = 1;
                    strFanHuiZhi = "所属报告分类为空，请检查！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行查询

                strSql = $@"
select A.*,
B.DanJia,
B.YouHuiJia,
B.ChengBenJia,
B.KeShiFenChengJia,
B.TiShiXinXi,
B.MoRenJianChaZhe,
B.MoRenShenHeZhe,
B.ShiFouDaoRuWaiJie
from jcsj_ZuHeXiangMu A ,jcsj_ZuHeXiangMuFenYuanQu B where A.ZuHeXiangMuID = B.ZuHeXiangMuID 
and ShenQingDanID='{strShenQingDanID}'
and B.SuoShuYuanQu = '{YuanQu}'
";
                if (strSuoShuBaoGaoFenLei != "公用")
                {
                    strSql += $"and (BaoGaoSuoShuFenLei='{strSuoShuBaoGaoFenLei}'or BaoGaoSuoShuFenLei='公用')";
                }

                strSql += $"order by ShunXu";

                ListZuHeXiangMu = new SqlSugarRepository<object>(db).getListBySql(strSql);

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ListZuHeXiangMu
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 组合项目维护明细项目添加
        /// </summary>
        /// <param name="MingXiXiangMuAddClass"></param>
        /// <param name="strYuanQuQianZhui"></param>
        /// <returns></returns>
        public string PostMingXiXiangMuAdd(MingXiXiangMuAddClass MingXiXiangMuAddClass, string strYuanQuQianZhui)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false; int iShunXu = 0; string strSql = ""; string strMingXiXiangMuID = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{MingXiXiangMuAddClass}】");

                #region 校验数据

                if (string.IsNullOrEmpty(MingXiXiangMuAddClass.MingXiXiangMuMingCheng))
                {
                    nret = 1;
                    strFanHuiZhi = "明细项目名称为空，请检查！";
                    goto PROC_EXIT;
                }

                if (string.IsNullOrEmpty(MingXiXiangMuAddClass.ZuHeXiangMuID))
                {
                    nret = 1;
                    strFanHuiZhi = "组合项目ID为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(MingXiXiangMuAddClass.JieGuoLeiXing))
                {
                    nret = 1;
                    strFanHuiZhi = "结果类型为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(MingXiXiangMuAddClass.ShiFouQiYong))
                {
                    nret = 1;
                    strFanHuiZhi = "是否启用为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(MingXiXiangMuAddClass.BaoGaoSuoShuFenLei))
                {
                    MingXiXiangMuAddClass.BaoGaoSuoShuFenLei = "公用";
                }

                if (string.IsNullOrEmpty(MingXiXiangMuAddClass.ShiYongXingBie))
                {
                    MingXiXiangMuAddClass.ShiYongXingBie = "不限";
                }

                #endregion

                #region 判断是否存在相同名称的明细项目

                List<jcsj_MingXiXiangMu> ListMingXiXiangMu = new SqlSugarRepository<jcsj_MingXiXiangMu>(db).getByWhere(a => a.MingXiXiangMuMingCheng == MingXiXiangMuAddClass.MingXiXiangMuMingCheng);

                if (ListMingXiXiangMu.Count != 0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前组合项目【{MingXiXiangMuAddClass.MingXiXiangMuMingCheng}】已存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 获得顺序


                List<jcsj_MingXiXiangMu> ListXinXi = new SqlSugarRepository<jcsj_MingXiXiangMu>(db).getByWhere(t => t.ZuHeXiangMuID == MingXiXiangMuAddClass.ZuHeXiangMuID);

                if (ListXinXi.Count != 0)
                {
                    iShunXu = ListXinXi.Max(t => t.ShunXu) + 1;
                }
                else
                {
                    iShunXu = 1;
                }

                #endregion

                #region 执行插入

                DbResult<bool> result = new SqlSugarRepository<jcsj_WeiHaiYinSuZuHeXiangMu>(db).UseTran(() =>
                {
                    jcsj_MingXiXiangMu MingXiXiangMu = new jcsj_MingXiXiangMu();

                    MingXiXiangMu.MingXiXiangMuMingCheng = MingXiXiangMuAddClass.MingXiXiangMuMingCheng.Trim();
                    MingXiXiangMu.ZuHeXiangMuID = MingXiXiangMuAddClass.ZuHeXiangMuID.Trim();
                    MingXiXiangMu.PYM = MingXiXiangMuAddClass.PYM.Trim();
                    MingXiXiangMu.ShiYongXingBie = MingXiXiangMuAddClass.ShiYongXingBie.Trim();
                    MingXiXiangMu.BaoGaoSuoShuFenLei = MingXiXiangMuAddClass.BaoGaoSuoShuFenLei.Trim();
                    MingXiXiangMu.JieGuoLeiXing = MingXiXiangMuAddClass.JieGuoLeiXing.Trim();
                    MingXiXiangMu.DanWei = MingXiXiangMuAddClass.DanWei.Trim();
                    MingXiXiangMu.CanKaoFanWei = MingXiXiangMuAddClass.CanKaoFanWei.Trim();
                    MingXiXiangMu.CanKaoXiaXian = MingXiXiangMuAddClass.CanKaoXiaXian.Trim();
                    MingXiXiangMu.CanKaoShangXian = MingXiXiangMuAddClass.CanKaoShangXian.Trim();
                    MingXiXiangMu.PianDiTiShi = MingXiXiangMuAddClass.PianDiTiShi.Trim();
                    MingXiXiangMu.PianGaoTiShi = MingXiXiangMuAddClass.PianGaoTiShi.Trim();
                    MingXiXiangMu.LisXiangMuBianMa = MingXiXiangMuAddClass.LisXiangMuBianMa.Trim();
                    MingXiXiangMu.BeiZhu1 = MingXiXiangMuAddClass.BeiZhu1.Trim();
                    MingXiXiangMu.BeiZhu2 = MingXiXiangMuAddClass.BeiZhu2.Trim();
                    MingXiXiangMu.ShengPingTaiBianMa = MingXiXiangMuAddClass.ShengPingTaiBianMa.Trim();
                    MingXiXiangMu.ShiFouQiYong = MingXiXiangMuAddClass.ShiFouQiYong.Trim();
                    MingXiXiangMu.ShunXu = iShunXu;

                    strMingXiXiangMuID = strYuanQuQianZhui + new SqlSugarRepository<jcsj_MingXiXiangMu>(db).AddReturnIdentity(MingXiXiangMu);

                    #region 执行修改语句将组合项目ID修改进去

                    strSql = $"update jcsj_MingXiXiangMu set MingXiXiangMuID='{strMingXiXiangMuID}' where MingXiXiangMuMingCheng='{MingXiXiangMuAddClass.MingXiXiangMuMingCheng}'";

                    BolShiFouChengGong = new SqlSugarRepository<jcsj_KeShi>(db).ExecuteCommandSql(strSql);

                    if (BolShiFouChengGong == false)
                    {
                        nret = 1;
                        strFanHuiZhi = "修改明细项目ID失败，请检查！";
                        return;
                    }

                    #endregion
                },
                 (ex) =>
                 {
                     nret = 1000;
                     strFanHuiZhi = ex.Message;
                     Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:事务报错异常:【{ex.Message}】");
                 });

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 组合项目维护明细项目修改
        /// </summary>
        /// <param name="MingXiXiangMuUpdateClass"></param>
        /// <returns></returns>
        public string PutMingXiXiangMuUpdate(MingXiXiangMuUpdateClass MingXiXiangMuUpdateClass)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false;
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{MingXiXiangMuUpdateClass}】");

                #region 数据校验

                if (string.IsNullOrEmpty(MingXiXiangMuUpdateClass.MingXiXiangMuMingCheng))
                {
                    nret = 1;
                    strFanHuiZhi = "明细项目名称为空，请检查！";
                    goto PROC_EXIT;
                }

                if (string.IsNullOrEmpty(MingXiXiangMuUpdateClass.ZuHeXiangMuID))
                {
                    nret = 1;
                    strFanHuiZhi = "组合项目ID为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(MingXiXiangMuUpdateClass.JieGuoLeiXing))
                {
                    nret = 1;
                    strFanHuiZhi = "结果类型为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(MingXiXiangMuUpdateClass.ShiFouQiYong))
                {
                    nret = 1;
                    strFanHuiZhi = "是否启用为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(MingXiXiangMuUpdateClass.BaoGaoSuoShuFenLei))
                {
                    MingXiXiangMuUpdateClass.BaoGaoSuoShuFenLei = "公用";
                }

                if (string.IsNullOrEmpty(MingXiXiangMuUpdateClass.ShiYongXingBie))
                {
                    MingXiXiangMuUpdateClass.ShiYongXingBie = "不限";
                }


                #endregion

                #region 判断是否存在相同名称的明细项目

                List<jcsj_MingXiXiangMu> ListMingXiXiangMu = new SqlSugarRepository<jcsj_MingXiXiangMu>(db)
                    .getByWhere(a => a.MingXiXiangMuMingCheng == MingXiXiangMuUpdateClass.MingXiXiangMuMingCheng
                    && a.ZuHeXiangMuID == MingXiXiangMuUpdateClass.ZuHeXiangMuID
                    && a.MingXiXiangMuID != MingXiXiangMuUpdateClass.MingXiXiangMuID);

                if (ListMingXiXiangMu.Count != 0)
                {
                    nret = 1;
                    strFanHuiZhi = $"当前明细项目【{MingXiXiangMuUpdateClass.MingXiXiangMuMingCheng}】已存在！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行修改语句

                BolShiFouChengGong = new SqlSugarRepository<jcsj_MingXiXiangMu>(db).Update(t => new jcsj_MingXiXiangMu
                {

                    MingXiXiangMuMingCheng = MingXiXiangMuUpdateClass.MingXiXiangMuMingCheng,
                    ZuHeXiangMuID = MingXiXiangMuUpdateClass.ZuHeXiangMuID,
                    PYM = MingXiXiangMuUpdateClass.PYM,
                    ShiYongXingBie = MingXiXiangMuUpdateClass.ShiYongXingBie,
                    BaoGaoSuoShuFenLei = MingXiXiangMuUpdateClass.BaoGaoSuoShuFenLei,
                    JieGuoLeiXing = MingXiXiangMuUpdateClass.JieGuoLeiXing,
                    DanWei = MingXiXiangMuUpdateClass.DanWei,
                    CanKaoFanWei = MingXiXiangMuUpdateClass.CanKaoFanWei,
                    CanKaoXiaXian = MingXiXiangMuUpdateClass.CanKaoXiaXian,
                    CanKaoShangXian = MingXiXiangMuUpdateClass.CanKaoShangXian,
                    PianDiTiShi = MingXiXiangMuUpdateClass.PianDiTiShi,
                    PianGaoTiShi = MingXiXiangMuUpdateClass.PianGaoTiShi,
                    LisXiangMuBianMa = MingXiXiangMuUpdateClass.LisXiangMuBianMa,
                    BeiZhu1 = MingXiXiangMuUpdateClass.BeiZhu1,
                    BeiZhu2 = MingXiXiangMuUpdateClass.BeiZhu2,
                    ShengPingTaiBianMa = MingXiXiangMuUpdateClass.ShengPingTaiBianMa,
                    ShiFouQiYong = MingXiXiangMuUpdateClass.ShiFouQiYong

                }, t => t.MingXiXiangMuID == MingXiXiangMuUpdateClass.MingXiXiangMuID);

                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修改明细项目失败，请检查！";
                    goto PROC_EXIT;
                }


            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 查询所有科室
        /// </summary>
        /// <param name="strYuanQu"></param>
        /// <param name="strUserID"></param>
        /// <returns></returns>
        public string GetKeShiXinXiSel(string strYuanQu, string strUserID)
        {
            int nret = 0; string strFanHuiZhi = ""; object KeShi = null;
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{strYuanQu},{strUserID}】");

                #region 校验数据

                nret = new MethodHelper(db).MD5Decrypt(out strFanHuiZhi, strUserID);
                if (nret != 0)
                {
                    nret = 1;
                    strFanHuiZhi = "加密值校验未通过!";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行查询

                if (strUserID == "hcwy")
                {
                    KeShi = new SqlSugarRepository<jcsj_KeShi>(db).getAll().Select(t => new
                    {
                        t.KeShiID,
                        t.KeShiMingCheng,
                        t.ShunXu
                    }).OrderBy(t => t.ShunXu).ToList();
                }
                else
                {
                    KeShi = new SqlSugarRepository<jcsj_KeShi>(db).getAll()
                        .Where(t => t.SuoShuYuanQu.Contains(strYuanQu))
                        .Select(t => new
                        {
                            t.KeShiID,
                            t.KeShiMingCheng,
                            t.ShunXu
                        }).OrderBy(t => t.ShunXu).ToList();
                }


            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = KeShi
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 组合项目维护明细项目删除
        /// </summary>
        /// <param name="strMingXiXiangMuID"></param>
        /// <returns></returns>
        public string DeleteMingXiXiangMuDel(string strMingXiXiangMuID)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false;
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{strMingXiXiangMuID}】");

                #region 数据校验

                if (string.IsNullOrEmpty(strMingXiXiangMuID))
                {
                    nret = 1;
                    strFanHuiZhi = "明细项目ID为空，请检查！";
                    goto PROC_EXIT;
                }

                #endregion

                BolShiFouChengGong = new SqlSugarRepository<jcsj_MingXiXiangMu>(db).DeleteWhere(A => A.MingXiXiangMuID == strMingXiXiangMuID);
                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "明细项目删除失败，请检查!";
                    goto PROC_EXIT;
                }

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 根据组合项目ID查询明细项目
        /// </summary>
        /// <param name="strZuHeXiangMuID"></param>
        /// <returns></returns>
        public string GetMingXiXiangMuSel(string strZuHeXiangMuID)
        {
            int nret = 0; string strFanHuiZhi = ""; List<jcsj_MingXiXiangMu> ListMingXi = new List<jcsj_MingXiXiangMu>();
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{strZuHeXiangMuID}】");

                #region 数据校验

                if (string.IsNullOrEmpty(strZuHeXiangMuID))
                {
                    nret = 1;
                    strFanHuiZhi = "组合项目ID为空，请检查！";
                    goto PROC_EXIT;
                }

                #region 执行查询

                ListMingXi = new SqlSugarRepository<jcsj_MingXiXiangMu>(db).getAll()
               .Where(t => t.ZuHeXiangMuID == strZuHeXiangMuID)
               .OrderBy(t => t.ShunXu).ToList();

            #endregion

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ListMingXi
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 调整申请单顺序
        /// </summary>
        /// <param name="iQianShunXu"></param>
        /// <param name="iHouShunXu"></param>
        /// <param name="ShenQingDanID"></param>
        /// <returns></returns>
        public string PutShenQingDanTiaoZhengShunXu(int iQianShunXu, int iHouShunXu, string ShenQingDanID)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{iQianShunXu},{iHouShunXu},{ShenQingDanID}】");

                if (new SqlSugarRepository<jcsj_ShenQingDan>(db).Update(t => new jcsj_ShenQingDan
                {
                    ShunXu = iHouShunXu
                }, t => t.ShenQingDanID == ShenQingDanID) == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修申请单顺序失败，请检查！";
                    goto PROC_EXIT;
                }
                Expression<Func<jcsj_ShenQingDan, jcsj_ShenQingDan>> columns;
                Expression<Func<jcsj_ShenQingDan, bool>> expression;
                //当修改前顺序小于修改后顺序时
                if (iQianShunXu < iHouShunXu)
                {
                    //修改顺序减1
                    columns = t => new jcsj_ShenQingDan
                    {
                        ShunXu = t.ShunXu - 1,
                    };
                    //条件为顺序字段大于等于修改前顺序并且小于等于修改后顺序并且不等于要修改的
                    expression = t => t.ShunXu >= iQianShunXu && t.ShunXu <= iHouShunXu && t.ShenQingDanID != ShenQingDanID;
                }
                //当修改前顺序大于修改后顺序时
                else if (iQianShunXu > iHouShunXu)
                {
                    //修改顺序加1
                    columns = t => new jcsj_ShenQingDan
                    {
                        ShunXu = t.ShunXu + 1,
                    };
                    //条件为顺序字段小于等于修改前顺序并且大于等于修改后顺序并且不等于要修改的
                    expression = t => t.ShunXu <= iQianShunXu && t.ShunXu >= iHouShunXu && t.ShenQingDanID != ShenQingDanID;
                }
                else
                {
                    nret = 1;
                    strFanHuiZhi = "调整前后顺序一致！";
                    goto PROC_EXIT;
                }

                if (new SqlSugarRepository<jcsj_ShenQingDan>(db).Update(columns, expression) == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修改申请单顺序失败，请检查！";
                    goto PROC_EXIT;
                }

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 调整组合项目顺序
        /// </summary>
        /// <param name="iQianShunXu"></param>
        /// <param name="iHouShunXu"></param>
        /// <param name="ZuHeXiangMuID"></param>
        /// <returns></returns>
        public string PutZuHeXiangMuTiaoZhengShunXu(int iQianShunXu, int iHouShunXu, string ZuHeXiangMuID)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{iQianShunXu},{iHouShunXu},{ZuHeXiangMuID}】");

                if (new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).Update(t => new jcsj_ZuHeXiangMu
                {
                    ShunXu = iHouShunXu
                }, t => t.ZuHeXiangMuID == ZuHeXiangMuID) == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修申请单顺序失败，请检查！";
                    goto PROC_EXIT;
                }
                Expression<Func<jcsj_ZuHeXiangMu, jcsj_ZuHeXiangMu>> columns;
                Expression<Func<jcsj_ZuHeXiangMu, bool>> expression;
                //当修改前顺序小于修改后顺序时
                if (iQianShunXu < iHouShunXu)
                {
                    //修改顺序减1
                    columns = t => new jcsj_ZuHeXiangMu
                    {
                        ShunXu = t.ShunXu - 1,
                    };
                    //条件为顺序字段大于等于修改前顺序并且小于等于修改后顺序并且不等于要修改的
                    expression = t => t.ShunXu >= iQianShunXu && t.ShunXu <= iHouShunXu && t.ZuHeXiangMuID != ZuHeXiangMuID;
                }
                //当修改前顺序大于修改后顺序时
                else if (iQianShunXu > iHouShunXu)
                {
                    //修改顺序加1
                    columns = t => new jcsj_ZuHeXiangMu
                    {
                        ShunXu = t.ShunXu + 1,
                    };
                    //条件为顺序字段小于等于修改前顺序并且大于等于修改后顺序并且不等于要修改的
                    expression = t => t.ShunXu <= iQianShunXu && t.ShunXu >= iHouShunXu && t.ZuHeXiangMuID != ZuHeXiangMuID;
                }
                else
                {
                    nret = 1;
                    strFanHuiZhi = "调整前后顺序一致！";
                    goto PROC_EXIT;
                }

                if (new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).Update(columns, expression) == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修改申请单顺序失败，请检查！";
                    goto PROC_EXIT;
                }

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 调整明细项目顺序
        /// </summary>
        /// <param name="iQianShunXu"></param>
        /// <param name="iHouShunXu"></param>
        /// <param name="MingXuXiangMuID"></param>
        /// <returns></returns>
        public string PutMingXiXiangMuTiaoZhengShunXu(int iQianShunXu, int iHouShunXu, string MingXiXiangMuID)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{iQianShunXu},{iHouShunXu},{MingXiXiangMuID}】");

                if (new SqlSugarRepository<jcsj_MingXiXiangMu>(db).Update(t => new jcsj_MingXiXiangMu
                {
                    ShunXu = iHouShunXu
                }, t => t.MingXiXiangMuID == MingXiXiangMuID) == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修申请单顺序失败，请检查！";
                    goto PROC_EXIT;
                }
                Expression<Func<jcsj_MingXiXiangMu, jcsj_MingXiXiangMu>> columns;
                Expression<Func<jcsj_MingXiXiangMu, bool>> expression;
                //当修改前顺序小于修改后顺序时
                if (iQianShunXu < iHouShunXu)
                {
                    //修改顺序减1
                    columns = t => new jcsj_MingXiXiangMu
                    {
                        ShunXu = t.ShunXu - 1,
                    };
                    //条件为顺序字段大于等于修改前顺序并且小于等于修改后顺序并且不等于要修改的
                    expression = t => t.ShunXu >= iQianShunXu && t.ShunXu <= iHouShunXu && t.MingXiXiangMuID != MingXiXiangMuID;
                }
                //当修改前顺序大于修改后顺序时
                else if (iQianShunXu > iHouShunXu)
                {
                    //修改顺序加1
                    columns = t => new jcsj_MingXiXiangMu
                    {
                        ShunXu = t.ShunXu + 1,
                    };
                    //条件为顺序字段小于等于修改前顺序并且大于等于修改后顺序并且不等于要修改的
                    expression = t => t.ShunXu <= iQianShunXu && t.ShunXu >= iHouShunXu && t.MingXiXiangMuID != MingXiXiangMuID;
                }
                else
                {
                    nret = 1;
                    strFanHuiZhi = "调整前后顺序一致！";
                    goto PROC_EXIT;
                }

                if (new SqlSugarRepository<jcsj_MingXiXiangMu>(db).Update(columns, expression) == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修改申请单顺序失败，请检查！";
                    goto PROC_EXIT;
                }

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 费用类别查询
        /// </summary>
        /// <returns></returns>
        public string GetFeiYongLeiBieSel()
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                var ListFeiYongLeiBie = new SqlSugarRepository<jcsj_ZiDian>(db).getByWhere(t => t.ErJiZiDianMingCheng == "费用类别")
                    .Select(t => new
                    {
                        t.ZiDianZhiNeiRong,
                        t.ZiDianID

                    });

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ListFeiYongLeiBie

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 检验性质查询
        /// </summary>
        /// <returns></returns>
        public string GetJianYanXingZhiSel()
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                var ListJianYanXingZhi = new SqlSugarRepository<jcsj_ZiDian>(db).getByWhere(t => t.ErJiZiDianMingCheng == "检验性质")
                    .Select(t => new
                    {
                        t.ZiDianZhiNeiRong,
                        t.ZiDianID

                    });

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ListJianYanXingZhi

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 组合项目检索值查询
        /// </summary>
        /// <param name="strKeShiID"></param>
        /// <returns></returns>
        public string GetJianSuoZuHeXiangMuSel(string strKeShiID, string strYuanQu)
        {
            int nret = 0; string strFanHuiZhi = ""; List<object> ListZuHeXiangMu = new List<object>(); string strSql = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{strKeShiID}】");

                #region 数据校验

                if (string.IsNullOrEmpty(strKeShiID))
                {
                    nret = 1;
                    strFanHuiZhi = "科室ID为空，请检查！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行查询

                strSql = $@"
select
(select KeShiMingCheng  from jcsj_KeShi where KeShiID=a.KeShiID) as 'KeShiMingCheng',
(select ShenQingDanMingCheng  from jcsj_ShenQingDan where ShenQingDanID=a.ShenQingDanID) as 'ShenQingDanMingCheng',
 a.ZuHeXiangMuMingCheng,
 b.DanJia,
 b.YouHuiJia,
 b.ChengBenjia,
 a.ZuHeXiangMuID,
 a.ShenQingDanID,
 a.KeShiID
from jcsj_ZuHeXiangMu a ,jcsj_ZuHeXiangMuFenYuanQu b  where a.ZuHeXiangMuID=b.ZuHeXiangMuID
and a.KeShiID='{strKeShiID}'
and b.SuoShuYuanQu='{strYuanQu}'
";
                ListZuHeXiangMu = new SqlSugarRepository<object>(db).getListBySql(strSql);

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ListZuHeXiangMu

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 调整申请单
        /// </summary>
        /// <param name="strXinShenQingDanID"></param>
        /// <param name="strZuHeXiangMuID"></param>
        /// <param name="strXinKeShiID"></param>
        /// <returns></returns>
        public string PutTiaoZhengZuHeXiangMu(string strXinShenQingDanID, string strZuHeXiangMuID, string strXinKeShiID)
        {
            int nret = 0; string strFanHuiZhi = ""; bool BolShiFouChengGong = false;
            List<object> ListZuHeXiangMu = new List<object>(); string strSql = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{strXinShenQingDanID},{strZuHeXiangMuID},{strXinKeShiID}】");

                #region 数据校验

                if (string.IsNullOrEmpty(strXinShenQingDanID))
                {
                    nret = 1;
                    strFanHuiZhi = "申请单ID为空，请检查!";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(strZuHeXiangMuID))
                {
                    nret = 1;
                    strFanHuiZhi = "组合项目ID为空，请检查!";
                    goto PROC_EXIT;
                }

                if (string.IsNullOrEmpty(strXinKeShiID))
                {
                    nret = 1;
                    strFanHuiZhi = "科室ID为空，请检查!";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行修改

                //修改 【jcsj_ZuHeXiangMu】
                strSql = $@"Update jcsj_ZuHeXiangMu set ShenQingDanID='{strXinShenQingDanID}',KeShiID='{strXinKeShiID}' where ZuHeXiangMuID='{strZuHeXiangMuID}'";

                BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMu>(db).ExecuteCommandSql(strSql);

                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "调整申请单失败，请检查";
                    goto PROC_EXIT;
                }

                //修改【ry_ShenQingDanZhu】
                strSql = $@"Update ry_ShenQingDanZhu set ShenQingDanID='{strXinShenQingDanID}' where ZuHeXiangMuIDJiHe like'%{strZuHeXiangMuID}%'";

                BolShiFouChengGong = new SqlSugarRepository<ry_ShenQingDanZhu>(db).ExecuteCommandSql(strSql);

                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修改人员申请单主失败，请检查";
                    goto PROC_EXIT;
                }

                //修改【ry_ShenQingDanMingXi】

                strSql = $@"Update ry_ShenQingDanMingXi set ShenQingDanID='{strXinShenQingDanID}' where ZuHeXiangMuID ='{strZuHeXiangMuID}'";

                BolShiFouChengGong = new SqlSugarRepository<ry_ShenQingDanMingXi>(db).ExecuteCommandSql(strSql);

                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "修改人员申请明细失败，请检查";
                    goto PROC_EXIT;
                }

            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 通用科室申请单查询
        /// </summary>
        /// <param name="strYuanQu"></param>
        /// <returns></returns>
        public string GetKeShiShenQingDanSel(string strYuanQu)
        {
            int nret = 0; string strFanHuiZhi = ""; List<QuanXianDataClass> listShenQingDan = new List<QuanXianDataClass>();

            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                List<jcsj_KeShi> listjcsj_KeShi = new SqlSugarRepository<jcsj_KeShi>(db).getByWhere(t => t.SuoShuYuanQu.Contains(strYuanQu)).OrderBy(t => t.ShunXu).ToList();
                foreach (var jcsj_KeShi in listjcsj_KeShi)
                {
                    QuanXianDataClass quanXianDataClass = new QuanXianDataClass();
                    quanXianDataClass.strCode = "_" + jcsj_KeShi.KeShiID.ToString();
                    quanXianDataClass.strName = jcsj_KeShi.KeShiMingCheng.ToString();
                    List<jcsj_ShenQingDan> listjcsj_ShenQingDan = new SqlSugarRepository<jcsj_ShenQingDan>(db).getByWhere(t => t.KeShiID == jcsj_KeShi.KeShiID && t.SuoShuYuanQu.Contains(strYuanQu))
                        .OrderBy(t => t.ShunXu).ToList();
                    List<ChildrenClass> listchildren = new List<ChildrenClass>();
                    foreach (var jcsj_ShenQingDan in listjcsj_ShenQingDan)
                    {
                        ChildrenClass children = new ChildrenClass();
                        children.strCode = jcsj_ShenQingDan.ShenQingDanID.ToString();
                        children.strName = jcsj_ShenQingDan.ShenQingDanMingCheng.ToString();
                        listchildren.Add(children);
                    }
                    if (listchildren.Count != 0)
                    {
                        quanXianDataClass.children = listchildren;
                        listShenQingDan.Add(quanXianDataClass);
                    }
                }


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = listShenQingDan

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 通用根据申请单ID查询组合项目
        /// </summary>
        /// <param name="strShenQingDanID"></param>
        /// <returns></returns>
        public string GetZuHeXiangMuTongYongSel(string strShenQingDanID, string strYuanQu)
        {
            int nret = 0; string strFanHuiZhi = ""; List<object> ListZuHeXiangMu = new List<object>();
            string strSql = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】接受参数：【{strShenQingDanID}】");

                strSql = $@"select A.*,
B.DanJia,
B.YouHuiJia,
B.ChengBenJia,
B.KeShiFenChengJia,
B.TiShiXinXi,
B.MoRenJianChaZhe,
B.MoRenShenHeZhe,
B.ShiFouDaoRuWaiJie
from jcsj_ZuHeXiangMu A ,jcsj_ZuHeXiangMuFenYuanQu B where A.ZuHeXiangMuID = B.ZuHeXiangMuID 
and ShenQingDanID='{strShenQingDanID}'
and b.SuoShuYuanQu='{strYuanQu}'
order by A.ShunXu";

                ListZuHeXiangMu = new SqlSugarRepository<object>(db).getListBySql(strSql);

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ListZuHeXiangMu

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 包含组合项目添加
        /// </summary>
        /// <param name="strZuHeXiangMuID"></param>
        /// <param name="strBaoHanZuHeXiangMuID"></param>
        /// <returns></returns>
        public string PostBaoHanZuHeXiangMuAdd(string strZuHeXiangMuID, string strBaoHanZuHeXiangMuID)
        {
            int nret = 0; string strFanHuiZhi = ""; List<object> ListZuHeXiangMu = new List<object>();
            bool BolShiFouChengGong = false; int iShunXu = 0;
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 数据校验

                if (string.IsNullOrEmpty(strZuHeXiangMuID))
                {
                    nret = 1;
                    strFanHuiZhi = "组合项目ID为空，请检查！";
                    goto PROC_EXIT;
                }
                if (string.IsNullOrEmpty(strBaoHanZuHeXiangMuID))
                {
                    nret = 1;
                    strFanHuiZhi = "包含组合项目ID为空，请检查！";
                    goto PROC_EXIT;
                }


                #endregion

                #region 获得顺序

                List<jcsj_ZuHeXiangMuBaoHanGuanXi> ListXinXi = new SqlSugarRepository<jcsj_ZuHeXiangMuBaoHanGuanXi>(db).getByWhere(t => t.ZuHeXiangMuID == strZuHeXiangMuID);

                if (ListXinXi.Count != 0)
                {
                    iShunXu = ListXinXi.Max(t => t.ShunXu) + 1;
                }
                else
                {
                    iShunXu = 1;
                }

                #endregion

                #region 处理包含组合项目ID

                string[] ArrBaoHanZuHeXiangMuID = strBaoHanZuHeXiangMuID.Split(';');

                foreach (string strBaoHanZuHeID in ArrBaoHanZuHeXiangMuID)
                {
                    if (string.IsNullOrEmpty(strBaoHanZuHeID) == false)
                    {
                        if (strZuHeXiangMuID == strBaoHanZuHeID)
                        {
                            nret = 1;
                            strFanHuiZhi = "组合项目Id和包含组合项目ID有一致数据";
                            goto PROC_EXIT;
                        }

                        #region 执行删除原有数据

                        //先删除后插入
                        BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMuBaoHanGuanXi>(db).DeleteWhere(A => A.ZuHeXiangMuID == strZuHeXiangMuID && A.BaoHanZuHeXiangMuID == strBaoHanZuHeID);

                        if (BolShiFouChengGong == false)
                        {
                            nret = 1;
                            strFanHuiZhi = "删除组合项目包含关系失败，请检查！";
                            goto PROC_EXIT;
                        }

                        #endregion

                        #region 执行增加jcsj_ShenQingDanFenYuanQu

                        jcsj_ZuHeXiangMuBaoHanGuanXi BaoHanGuanXi = new jcsj_ZuHeXiangMuBaoHanGuanXi();

                        BaoHanGuanXi.ZuHeXiangMuID = strZuHeXiangMuID;
                        BaoHanGuanXi.BaoHanZuHeXiangMuID = strBaoHanZuHeID;
                        BaoHanGuanXi.ShunXu = iShunXu;


                        BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMuBaoHanGuanXi>(db).Add(BaoHanGuanXi);
                        if (BolShiFouChengGong == false)
                        {
                            nret = 1;
                            strFanHuiZhi = "添加包含组合项目出现错误，请检查！";
                            goto PROC_EXIT;
                        }

                        #endregion
                    }
                }
            #endregion


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 包含组合项目查询
        /// </summary>
        /// <param name="strZuHeXiangMuID"></param>
        /// <returns></returns>
        public string GetBaoHanZuHeXiangMuSel(string strZuHeXiangMuID)
        {
            int nret = 0; string strFanHuiZhi = ""; List<object> ListBaoHanZuHeXiangMu = new List<object>();
            string strSql = "";
            try
            {

                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】接受参数：【{strZuHeXiangMuID}】");

                #region 数据校验

                if (string.IsNullOrEmpty(strZuHeXiangMuID))
                {
                    nret = 1;
                    strFanHuiZhi = "组合项目ID为空，请检查！";
                    goto PROC_EXIT;
                }

                #endregion

                strSql = $@"Select *,
(select ZuHeXiangMuMingCheng from jcsj_ZuHeXiangMu
where ZuHeXiangMuID =a.BaoHanZuHeXiangMuID) as ZuHeXiangMuMingCheng
from jcsj_ZuHeXiangMuBaoHanGuanXi a
where ZuHeXiangMuID='{strZuHeXiangMuID}'
order by ShunXu";

                ListBaoHanZuHeXiangMu = new SqlSugarRepository<object>(db).getListBySql(strSql);


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ListBaoHanZuHeXiangMu

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 包含组合项目删除
        /// </summary>
        /// <param name="strBaoHanGuanXiID"></param>
        /// <returns></returns>
        public string DeleteBaoHanZuHeXiangMuDel(string strBaoHanGuanXiID)
        {
            int nret = 0; string strFanHuiZhi = "";
            bool BolShiFouChengGong = false;
            try
            {

                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】接受参数：【{strBaoHanGuanXiID}】");

                #region 数据校验

                if (string.IsNullOrEmpty(strBaoHanGuanXiID))
                {
                    nret = 1;
                    strFanHuiZhi = "包含关系ID为空，请检查！";
                    goto PROC_EXIT;
                }

                #endregion

                #region 执行删除

                BolShiFouChengGong = new SqlSugarRepository<jcsj_ZuHeXiangMuBaoHanGuanXi>(db).DeleteWhere(A => A.BaoHanGuanXiID == int.Parse(strBaoHanGuanXiID));

                if (BolShiFouChengGong == false)
                {
                    nret = 1;
                    strFanHuiZhi = "删除组合项目包含关系失败，请检查！";
                    goto PROC_EXIT;
                }

            #endregion



            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""

                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
    }
}
